package org.whatever.aha.zjut.platform.service.project;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.entity.project.ProjectAppJudge;
import org.whatever.aha.zjut.platform.mapper.project.ProjectAppJudgeMapper;
import org.whatever.aha.zjut.platform.mapper.project.ProjectMapper;
import org.whatever.aha.zjut.platform.util.StatusConvertUtil;
import org.whatever.aha.zjut.platform.vo.PageVo;
import org.whatever.aha.zjut.platform.vo.info.StatusInfoVo;
import org.whatever.aha.zjut.platform.vo.project.ProjectAppJudgeVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Vc
 * @Desc
 * @date 2021/09/15 0:02
 */

@RequiredArgsConstructor
@Service
public class ProjectAppJudgeService {

    final ProjectAppJudgeMapper projectAppJudgeMapper;
    final ProjectMapper projectMapper;
    final ProjectService projectService;

    /**
     * 分页条件查询申请评委意见审批结果
     *
     * @param projectIds
     * @param applyStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageVo<List<ProjectAppJudgeVo>> getProjectAppJudgePagable(List<Object> projectIds, Integer applyStatus, int pageNum, int pageSize) {
        Page<ProjectAppJudge> projectPage = projectAppJudgeMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<ProjectAppJudge>().in(CollectionUtil.isNotEmpty(projectIds), "project_id", projectIds).eq(applyStatus!=null, "apply_status", applyStatus));
        List<ProjectAppJudgeVo> collect = projectPage.getRecords().stream().map(projectAppJudge -> {
            ProjectAppJudgeVo projectAppJudgeVo = new ProjectAppJudgeVo();
            BeanUtils.copyProperties(projectAppJudge, projectAppJudgeVo);
            projectAppJudgeVo.setProjectInfo(projectService.getProjectRoughVoById(projectAppJudge.getProjectId()));
            projectAppJudgeVo.setApplyStatusInfo(new StatusInfoVo(projectAppJudge.getApplyStatus(), StatusConvertUtil.convertProjectApplicationStatus(projectAppJudge.getApplyStatus())));
            return projectAppJudgeVo;
        }).collect(Collectors.toList());
        return new PageVo<>(projectPage.getCurrent(), projectPage.getSize(),collect , projectPage.getTotal());
    }

    /**
     * 学生增加一个申请
     *
     * @param projectId
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int addProjectAppJudge(int projectId){
        if(projectAppJudgeMapper.selectCount(new QueryWrapper<ProjectAppJudge>().eq("project_id", projectId)) > 0){
            throw new AppException(ErrorCode.PROJECT_APP_JUDGE_LIMIT);
        }
        ProjectAppJudge projectAppJudge = ProjectAppJudge.builder().projectId(projectId)
                .applyStatus(StatusConstant.COMPETITION_APP_JUDGE_STATUS_WAIT_TO_CHECK.getCode())
                .updateTime(new Date()).build();
        projectAppJudgeMapper.insert(projectAppJudge);
        return projectAppJudge.getProjectApplyId();
    }

    /**
     * 院级管理员审批申请
     *
     * @param projectId
     * @param applyStatus
     * @param applyComment
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public Integer approveProjectAppJudge(int projectId, int applyStatus, String applyComment){
        UpdateWrapper<ProjectAppJudge> update = new UpdateWrapper<ProjectAppJudge>().eq("project_id", projectId)
                .set("apply_status", applyStatus)
                .set("apply_comment", applyComment)
                .set("update_time", new Date());
        return projectAppJudgeMapper.update(null, update);
    }

    /**
     * 根据申请项目id对查询进行撤回/删除
     *
     * @param projectId
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public Integer withdrawProjectAppJudge(int projectId){
        return projectAppJudgeMapper.delete(new QueryWrapper<ProjectAppJudge>().eq("project_id", projectId));
    }

    /**
     * 根据项目Id获取申请状态
     *
     * @param projectId
     * @return
     */
    public Integer getApplyStatusByProjectId(int projectId){
        return projectAppJudgeMapper.selectOne(new QueryWrapper<ProjectAppJudge>().eq("project_id", projectId)).getApplyStatus();
    }
}